<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li class="active"><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. plugin">1. plugin</a><ul><li><a href="#t11.1 为什么需要一个插件">1.1 为什么需要一个插件</a></li><li><a href="#t21.2 可以加载插件的常用对象">1.2 可以加载插件的常用对象</a></li></ul></li><li><a href="#t32. 创建插件">2. 创建插件</a></li><li><a href="#t43. Compiler 和 Compilation">3. Compiler 和 Compilation</a></li><li><a href="#t54. 基本插件架构">4. 基本插件架构</a></li><li><a href="#t65. 访问 compilation 对象">5. 访问 compilation 对象</a></li><li><a href="#t76. 异步编译插件">6. 异步编译插件</a></li><li><a href="#t87. 输出文件列表">7. 输出文件列表</a></li><li><a href="#t98. InlineWebpackPlugin">8. InlineWebpackPlugin</a></li><li><a href="#t109.自动上传资源文件到CDN">9.自动上传资源文件到CDN</a></li><li><a href="#t1110.自动外链">10.自动外链</a><ul><li><a href="#t1210.1 使用外部类库">10.1 使用外部类库</a></li><li><a href="#t1310.2 思路">10.2 思路</a></li></ul></li><li><a href="#t1410.3 使用plugins">10.3 使用plugins</a></li><li><a href="#t15参考">参考</a><ul><li><a href="#t16核心流程">核心流程</a></li></ul></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. plugin">1. plugin <a href="#t01. plugin"> # </a></h2>
<p>&#x63D2;&#x4EF6;&#x5411;&#x7B2C;&#x4E09;&#x65B9;&#x5F00;&#x53D1;&#x8005;&#x63D0;&#x4F9B;&#x4E86; webpack &#x5F15;&#x64CE;&#x4E2D;&#x5B8C;&#x6574;&#x7684;&#x80FD;&#x529B;&#x3002;&#x4F7F;&#x7528;&#x9636;&#x6BB5;&#x5F0F;&#x7684;&#x6784;&#x5EFA;&#x56DE;&#x8C03;&#xFF0C;&#x5F00;&#x53D1;&#x8005;&#x53EF;&#x4EE5;&#x5F15;&#x5165;&#x5B83;&#x4EEC;&#x81EA;&#x5DF1;&#x7684;&#x884C;&#x4E3A;&#x5230; webpack &#x6784;&#x5EFA;&#x6D41;&#x7A0B;&#x4E2D;&#x3002;&#x521B;&#x5EFA;&#x63D2;&#x4EF6;&#x6BD4;&#x521B;&#x5EFA; loader &#x66F4;&#x52A0;&#x9AD8;&#x7EA7;&#xFF0C;&#x56E0;&#x4E3A;&#x4F60;&#x5C06;&#x9700;&#x8981;&#x7406;&#x89E3;&#x4E00;&#x4E9B; webpack &#x5E95;&#x5C42;&#x7684;&#x5185;&#x90E8;&#x7279;&#x6027;&#x6765;&#x505A;&#x76F8;&#x5E94;&#x7684;&#x94A9;&#x5B50;</p>
<h3 id="t11.1 &#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x63D2;&#x4EF6;">1.1 &#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x63D2;&#x4EF6; <a href="#t11.1 &#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x63D2;&#x4EF6;"> # </a></h3>
<ul>
<li>webpack&#x57FA;&#x7840;&#x914D;&#x7F6E;&#x65E0;&#x6CD5;&#x6EE1;&#x8DB3;&#x9700;&#x6C42;</li>
<li>&#x63D2;&#x4EF6;&#x51E0;&#x4E4E;&#x80FD;&#x591F;&#x4EFB;&#x610F;&#x66F4;&#x6539;webpack&#x7F16;&#x8BD1;&#x7ED3;&#x679C;</li>
<li>webpack&#x5185;&#x90E8;&#x4E5F;&#x662F;&#x901A;&#x8FC7;&#x5927;&#x91CF;&#x5185;&#x90E8;&#x63D2;&#x4EF6;&#x5B9E;&#x73B0;&#x7684;</li>
</ul>
<h3 id="t21.2 &#x53EF;&#x4EE5;&#x52A0;&#x8F7D;&#x63D2;&#x4EF6;&#x7684;&#x5E38;&#x7528;&#x5BF9;&#x8C61;">1.2 &#x53EF;&#x4EE5;&#x52A0;&#x8F7D;&#x63D2;&#x4EF6;&#x7684;&#x5E38;&#x7528;&#x5BF9;&#x8C61; <a href="#t21.2 &#x53EF;&#x4EE5;&#x52A0;&#x8F7D;&#x63D2;&#x4EF6;&#x7684;&#x5E38;&#x7528;&#x5BF9;&#x8C61;"> # </a></h3>
<table>
<thead>
<tr>
<th style="text-align:left">&#x5BF9;&#x8C61;</th>
<th style="text-align:left">&#x94A9;&#x5B50;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">Compiler</td>
<td style="text-align:left">run,compile,compilation,make,emit,done</td>
</tr>
<tr>
<td style="text-align:left">Compilation</td>
<td style="text-align:left">buildModule,normalModuleLoader,succeedModule,finishModules,seal,optimize,after-seal</td>
</tr>
<tr>
<td style="text-align:left">Module Factory</td>
<td style="text-align:left">beforeResolver,afterResolver,module,parser</td>
</tr>
<tr>
<td style="text-align:left">Module</td>
<td style="text-align:left"></td>
</tr>
<tr>
<td style="text-align:left">Parser</td>
<td style="text-align:left">program,statement,call,expression</td>
</tr>
<tr>
<td style="text-align:left">Template</td>
<td style="text-align:left">hash,bootstrap,localVars,render</td>
</tr>
</tbody>
</table>
<h2 id="t32. &#x521B;&#x5EFA;&#x63D2;&#x4EF6;">2. &#x521B;&#x5EFA;&#x63D2;&#x4EF6; <a href="#t32. &#x521B;&#x5EFA;&#x63D2;&#x4EF6;"> # </a></h2>
<p>webpack &#x63D2;&#x4EF6;&#x7531;&#x4EE5;&#x4E0B;&#x7EC4;&#x6210;&#xFF1A;</p>
<ul>
<li>&#x4E00;&#x4E2A; JavaScript &#x547D;&#x540D;&#x51FD;&#x6570;&#x3002;</li>
<li>&#x5728;&#x63D2;&#x4EF6;&#x51FD;&#x6570;&#x7684; prototype &#x4E0A;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A; apply &#x65B9;&#x6CD5;&#x3002;</li>
<li>&#x6307;&#x5B9A;&#x4E00;&#x4E2A;&#x7ED1;&#x5B9A;&#x5230; webpack &#x81EA;&#x8EAB;&#x7684;&#x4E8B;&#x4EF6;&#x94A9;&#x5B50;&#x3002;</li>
<li>&#x5904;&#x7406; webpack &#x5185;&#x90E8;&#x5B9E;&#x4F8B;&#x7684;&#x7279;&#x5B9A;&#x6570;&#x636E;&#x3002;</li>
<li>&#x529F;&#x80FD;&#x5B8C;&#x6210;&#x540E;&#x8C03;&#x7528; webpack &#x63D0;&#x4F9B;&#x7684;&#x56DE;&#x8C03;&#x3002;</li>
</ul>
<h2 id="t43. Compiler &#x548C; Compilation">3. Compiler &#x548C; Compilation <a href="#t43. Compiler &#x548C; Compilation"> # </a></h2>
<p>&#x5728;&#x63D2;&#x4EF6;&#x5F00;&#x53D1;&#x4E2D;&#x6700;&#x91CD;&#x8981;&#x7684;&#x4E24;&#x4E2A;&#x8D44;&#x6E90;&#x5C31;&#x662F;<code>compiler</code>&#x548C;<code>compilation</code>&#x5BF9;&#x8C61;&#x3002;&#x7406;&#x89E3;&#x5B83;&#x4EEC;&#x7684;&#x89D2;&#x8272;&#x662F;&#x6269;&#x5C55;webpack&#x5F15;&#x64CE;&#x91CD;&#x8981;&#x7684;&#x7B2C;&#x4E00;&#x6B65;&#x3002;</p>
<ul>
<li><p>compiler &#x5BF9;&#x8C61;&#x4EE3;&#x8868;&#x4E86;&#x5B8C;&#x6574;&#x7684; webpack &#x73AF;&#x5883;&#x914D;&#x7F6E;&#x3002;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x5728;&#x542F;&#x52A8; webpack &#x65F6;&#x88AB;&#x4E00;&#x6B21;&#x6027;&#x5EFA;&#x7ACB;&#xFF0C;&#x5E76;&#x914D;&#x7F6E;&#x597D;&#x6240;&#x6709;&#x53EF;&#x64CD;&#x4F5C;&#x7684;&#x8BBE;&#x7F6E;&#xFF0C;&#x5305;&#x62EC; options&#xFF0C;loader &#x548C; plugin&#x3002;&#x5F53;&#x5728; webpack &#x73AF;&#x5883;&#x4E2D;&#x5E94;&#x7528;&#x4E00;&#x4E2A;&#x63D2;&#x4EF6;&#x65F6;&#xFF0C;&#x63D2;&#x4EF6;&#x5C06;&#x6536;&#x5230;&#x6B64; compiler &#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x3002;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5B83;&#x6765;&#x8BBF;&#x95EE; webpack &#x7684;&#x4E3B;&#x73AF;&#x5883;&#x3002;</p>
</li>
<li><p>compilation &#x5BF9;&#x8C61;&#x4EE3;&#x8868;&#x4E86;&#x4E00;&#x6B21;&#x8D44;&#x6E90;&#x7248;&#x672C;&#x6784;&#x5EFA;&#x3002;&#x5F53;&#x8FD0;&#x884C; webpack &#x5F00;&#x53D1;&#x73AF;&#x5883;&#x4E2D;&#x95F4;&#x4EF6;&#x65F6;&#xFF0C;&#x6BCF;&#x5F53;&#x68C0;&#x6D4B;&#x5230;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x53D8;&#x5316;&#xFF0C;&#x5C31;&#x4F1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684; compilation&#xFF0C;&#x4ECE;&#x800C;&#x751F;&#x6210;&#x4E00;&#x7EC4;&#x65B0;&#x7684;&#x7F16;&#x8BD1;&#x8D44;&#x6E90;&#x3002;&#x4E00;&#x4E2A; compilation &#x5BF9;&#x8C61;&#x8868;&#x73B0;&#x4E86;&#x5F53;&#x524D;&#x7684;&#x6A21;&#x5757;&#x8D44;&#x6E90;&#x3001;&#x7F16;&#x8BD1;&#x751F;&#x6210;&#x8D44;&#x6E90;&#x3001;&#x53D8;&#x5316;&#x7684;&#x6587;&#x4EF6;&#x3001;&#x4EE5;&#x53CA;&#x88AB;&#x8DDF;&#x8E2A;&#x4F9D;&#x8D56;&#x7684;&#x72B6;&#x6001;&#x4FE1;&#x606F;&#x3002;compilation &#x5BF9;&#x8C61;&#x4E5F;&#x63D0;&#x4F9B;&#x4E86;&#x5F88;&#x591A;&#x5173;&#x952E;&#x65F6;&#x673A;&#x7684;&#x56DE;&#x8C03;&#xFF0C;&#x4EE5;&#x4F9B;&#x63D2;&#x4EF6;&#x505A;&#x81EA;&#x5B9A;&#x4E49;&#x5904;&#x7406;&#x65F6;&#x9009;&#x62E9;&#x4F7F;&#x7528;&#x3002;</p>
</li>
<li><p><a href="https://github.com/webpack/webpack/blob/master/lib/Compiler.js">Compiler</a></p>
</li>
<li><a href="https://github.com/webpack/webpack/blob/master/lib/Compilation.js">Compilation</a></li>
<li><a href="https://github.com/webpack/webpack/blob/master/lib/Parser.js">Parser</a></li>
<li><a href="https://github.com/webpack/webpack/blob/master/lib/NormalModuleFactory.js">NormalModuleFactory</a></li>
</ul>
<h2 id="t54. &#x57FA;&#x672C;&#x63D2;&#x4EF6;&#x67B6;&#x6784;">4. &#x57FA;&#x672C;&#x63D2;&#x4EF6;&#x67B6;&#x6784; <a href="#t54. &#x57FA;&#x672C;&#x63D2;&#x4EF6;&#x67B6;&#x6784;"> # </a></h2>
<ul>
<li>&#x63D2;&#x4EF6;&#x662F;&#x7531;&#x300C;&#x5177;&#x6709; apply &#x65B9;&#x6CD5;&#x7684; prototype &#x5BF9;&#x8C61;&#x300D;&#x6240;&#x5B9E;&#x4F8B;&#x5316;&#x51FA;&#x6765;&#x7684;&#x3002;</li>
<li>&#x8FD9;&#x4E2A; apply &#x65B9;&#x6CD5;&#x5728;&#x5B89;&#x88C5;&#x63D2;&#x4EF6;&#x65F6;&#xFF0C;&#x4F1A;&#x88AB; webpack compiler &#x8C03;&#x7528;&#x4E00;&#x6B21;&#x3002;</li>
<li>apply &#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x63A5;&#x6536;&#x4E00;&#x4E2A; webpack compiler &#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#xFF0C;&#x4ECE;&#x800C;&#x53EF;&#x4EE5;&#x5728;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4E2D;&#x8BBF;&#x95EE;&#x5230; compiler &#x5BF9;&#x8C61;&#x3002;</li>
</ul>
<p>webpack/lib/webpack.js:35</p>
<pre><code class="lang-js"><span class="hljs-keyword">if</span> (options.plugins &amp;&amp; <span class="hljs-built_in">Array</span>.isArray(options.plugins)) {
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> plugin <span class="hljs-keyword">of</span> options.plugins) {
        plugin.apply(compiler);
    }
}
</code></pre>
<p>&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x63D2;&#x4EF6;&#x7ED3;&#x6784;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DonePlugin</span></span>{
    <span class="hljs-keyword">constructor</span>(options) {
        <span class="hljs-keyword">this</span>.options=options;
    }
    apply(compiler) {
        compiler.hooks.done.tap(<span class="hljs-string">&apos;DonePlugin&apos;</span>, ()=&gt; {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;Hello &apos;</span>,<span class="hljs-keyword">this</span>.options.name);
        });
    }
}
<span class="hljs-built_in">module</span>.exports=DonePlugin;
</code></pre>
<p>&#x7136;&#x540E;&#xFF0C;&#x8981;&#x5B89;&#x88C5;&#x8FD9;&#x4E2A;&#x63D2;&#x4EF6;&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x5728;&#x4F60;&#x7684; webpack &#x914D;&#x7F6E;&#x7684; plugin &#x6570;&#x7EC4;&#x4E2D;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> DonePlugin=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./plugins/DonePlugin&apos;</span>);
<span class="hljs-built_in">module</span>.exports={
    <span class="hljs-attr">entry</span>: <span class="hljs-string">&apos;./src/index.js&apos;</span>,
    <span class="hljs-attr">output</span>: {
        <span class="hljs-attr">path</span>: path.resolve(<span class="hljs-string">&apos;build&apos;</span>),
        <span class="hljs-attr">filename</span>:<span class="hljs-string">&apos;bundle.js&apos;</span>
    },
    <span class="hljs-attr">plugins</span>: [
        <span class="hljs-keyword">new</span> DonePlugin({<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx&apos;</span>})
    ]
}
</code></pre>
<p>webpack/lib/Compiler.js:251</p>
<pre><code class="lang-js"><span class="hljs-keyword">this</span>.emitRecords(<span class="hljs-function"><span class="hljs-params">err</span> =&gt;</span> {
    <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">return</span> finalCallback(err);
        <span class="hljs-keyword">this</span>.hooks.done.callAsync(stats, err =&gt; {});
});
</code></pre>
<h2 id="t65. &#x8BBF;&#x95EE; compilation &#x5BF9;&#x8C61;">5. &#x8BBF;&#x95EE; compilation &#x5BF9;&#x8C61; <a href="#t65. &#x8BBF;&#x95EE; compilation &#x5BF9;&#x8C61;"> # </a></h2>
<p>&#x4F7F;&#x7528; compiler &#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x7ED1;&#x5B9A;&#x63D0;&#x4F9B;&#x4E86;&#x7F16;&#x8BD1; compilation &#x5F15;&#x7528;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x7136;&#x540E;&#x62FF;&#x5230;&#x6BCF;&#x6B21;&#x65B0;&#x7684; compilation &#x5BF9;&#x8C61;&#x3002;&#x8FD9;&#x4E9B; compilation &#x5BF9;&#x8C61;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E9B;&#x94A9;&#x5B50;&#x51FD;&#x6570;&#xFF0C;&#x6765;&#x94A9;&#x5165;&#x5230;&#x6784;&#x5EFA;&#x6D41;&#x7A0B;&#x7684;&#x5F88;&#x591A;&#x6B65;&#x9AA4;&#x4E2D;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CompilationPlugin</span></span>{
    <span class="hljs-keyword">constructor</span>(options) {
        <span class="hljs-keyword">this</span>.options=options;
    }
    apply(compiler) {
        compiler.hooks.compilation.tap(<span class="hljs-string">&apos;CompilationPlugin&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">compilation</span>) </span>{
            compilation.hooks.optimize.tap(<span class="hljs-string">&apos;optimize&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x8D44;&#x6E90;&#x6B63;&#x5728;&#x88AB;&#x4F18;&#x5316;&apos;</span>);
            });
        });
    }
}
<span class="hljs-built_in">module</span>.exports=CompilationPlugin;

</code></pre>
<p>webpack/lib/Compiler.js:496</p>
<pre><code class="lang-js">newCompilation(params) {
        <span class="hljs-keyword">const</span> compilation = <span class="hljs-keyword">this</span>.createCompilation();
        <span class="hljs-keyword">this</span>.hooks.compilation.call(compilation, params);
        <span class="hljs-keyword">return</span> compilation;
    }
</code></pre>
<p>webpack/lib/Compilation.js:1183</p>
<pre><code class="lang-js">seal(callback) {
        <span class="hljs-keyword">this</span>.hooks.seal.call();
        <span class="hljs-keyword">this</span>.hooks.optimize.call();
}
</code></pre>
<p>&#x5173;&#x4E8E; compiler, compilation &#x7684;&#x53EF;&#x7528;&#x56DE;&#x8C03;&#xFF0C;&#x548C;&#x5176;&#x5B83;&#x91CD;&#x8981;&#x7684;&#x5BF9;&#x8C61;&#x7684;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x67E5;&#x770B; <a href="https://doc.webpack-china.org/api/plugins/">&#x63D2;&#x4EF6;</a> &#x6587;&#x6863;&#x3002; </p>
<h2 id="t76. &#x5F02;&#x6B65;&#x7F16;&#x8BD1;&#x63D2;&#x4EF6;">6. &#x5F02;&#x6B65;&#x7F16;&#x8BD1;&#x63D2;&#x4EF6; <a href="#t76. &#x5F02;&#x6B65;&#x7F16;&#x8BD1;&#x63D2;&#x4EF6;"> # </a></h2>
<ul>
<li>&#x6709;&#x4E00;&#x4E9B;&#x7F16;&#x8BD1;&#x63D2;&#x4EF6;&#x4E2D;&#x7684;&#x6B65;&#x9AA4;&#x662F;&#x5F02;&#x6B65;&#x7684;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x9700;&#x8981;&#x989D;&#x5916;&#x4F20;&#x5165;&#x4E00;&#x4E2A; callback &#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x5E76;&#x4E14;&#x5728;&#x63D2;&#x4EF6;&#x8FD0;&#x884C;&#x7ED3;&#x675F;&#x65F6;&#xFF0C;&#x5FC5;&#x987B;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x3002;</li>
</ul>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CompilationAsyncPlugin</span></span>{
    <span class="hljs-keyword">constructor</span>(options) {
        <span class="hljs-keyword">this</span>.options=options;
    }
    apply(compiler) {
        compiler.hooks.emit.tapAsync(<span class="hljs-string">&apos;EmitPlugin&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">compilation,callback</span>) </span>{
            setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
                <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5F02;&#x6B65;&#x4EFB;&#x52A1;&#x5B8C;&#x6210;&apos;</span>);
                callback();
            },<span class="hljs-number">500</span>);
        });
    }
}
<span class="hljs-built_in">module</span>.exports=CompilationAsyncPlugin;
</code></pre>
<p><code>emit</code>&#x4E8B;&#x4EF6;&#x5728;&#x5373;&#x5C06;&#x5199;&#x5165;&#x6587;&#x4EF6;&#x524D;&#x89E6;&#x53D1; 
webpack/lib/Compiler.js:364</p>
<pre><code class="lang-js"><span class="hljs-keyword">this</span>.hooks.emit.callAsync(compilation, err =&gt; {
    <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">return</span> callback(err);
    outputPath = compilation.getPath(<span class="hljs-keyword">this</span>.outputPath);
    <span class="hljs-keyword">this</span>.outputFileSystem.mkdirp(outputPath, emitFiles);
});
</code></pre>
<h2 id="t87. &#x8F93;&#x51FA;&#x6587;&#x4EF6;&#x5217;&#x8868;">7. &#x8F93;&#x51FA;&#x6587;&#x4EF6;&#x5217;&#x8868; <a href="#t87. &#x8F93;&#x51FA;&#x6587;&#x4EF6;&#x5217;&#x8868;"> # </a></h2>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FileListPlugin</span></span>{
    <span class="hljs-keyword">constructor</span>(options) {
        <span class="hljs-keyword">this</span>.options = options;
    }
    apply(compiler) {
        compiler.hooks.emit.tap(<span class="hljs-string">&apos;FileListPlugin&apos;</span>, (compilation) =&gt;{
            <span class="hljs-keyword">let</span> filelist=<span class="hljs-string">&apos;## &#x6587;&#x4EF6;&#x5217;&#x8868;&apos;</span>;
            filelist = <span class="hljs-built_in">Object</span>.keys(compilation.assets).reduce(<span class="hljs-function">(<span class="hljs-params">filelist,filename</span>)=&gt;</span>filelist+<span class="hljs-string">&apos;\r\n- &apos;</span>+filename,filelist);
            compilation.assets[<span class="hljs-keyword">this</span>.options.name?<span class="hljs-keyword">this</span>.options.name:<span class="hljs-string">&apos;filelist.md&apos;</span>]={
                source() {
                    <span class="hljs-keyword">return</span> filelist;
                },
                size() {
                    <span class="hljs-keyword">return</span> filelist.length
                }
            }
        });
    }
}
<span class="hljs-built_in">module</span>.exports=FileListPlugin;
</code></pre>
<h2 id="t98. InlineWebpackPlugin">8. InlineWebpackPlugin <a href="#t98. InlineWebpackPlugin"> # </a></h2>
<p>&#x6709;&#x4E9B;&#x65F6;&#x5019;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x628A;&#x811A;&#x672C;&#x548C;&#x6837;&#x5F0F;&#x5355;&#x72EC;&#x5185;&#x8054;&#x5728;HTML&#x9875;&#x9762;&#x91CC;&#x9762;</p>
<ul>
<li><a href="https://github.com/jantimon/html-webpack-plugin">html-webpack-plugin</a></li>
<li><a href="https://github.com/DustinJackson/html-webpack-inline-source-plugin">html-webpack-inline-source-plugin</a></li>
</ul>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">InlineWebpackPlugin</span> </span>{
    <span class="hljs-keyword">constructor</span>(options) {
        <span class="hljs-keyword">this</span>.options = options;
    }
    apply(compiler) {
        compiler.hooks.compilation.tap(<span class="hljs-string">&apos;InlineWebpackPlugin&apos;</span>, compilation =&gt; {
            compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync(<span class="hljs-string">&apos;InlineWebpackPlugin&apos;</span>, (htmlPluginData, callback) =&gt; {
                <span class="hljs-keyword">if</span> (!<span class="hljs-keyword">this</span>.options.inlineSource) {
                    <span class="hljs-keyword">return</span> callback(<span class="hljs-literal">null</span>, htmlPluginData);
                }
                <span class="hljs-built_in">console</span>.log(htmlPluginData);
                <span class="hljs-comment">/**
                 * { head:[ { tagName: &apos;link&apos;,
                              selfClosingTag: false,
                              voidTag: true,
                              attributes: [Object] } ],
                    body: [ { tagName: &apos;script&apos;, closeTag: true, attributes: [Object] } ],
                 */</span>
                htmlPluginData = <span class="hljs-keyword">this</span>.processTags(compilation, htmlPluginData);
                callback(<span class="hljs-literal">null</span>, htmlPluginData);
            });
        });
    }
    processTags(compilation, htmlPluginData) {
        htmlPluginData.head = htmlPluginData.head.map(<span class="hljs-function"><span class="hljs-params">tag</span> =&gt;</span> <span class="hljs-keyword">this</span>.processTag(compilation, tag));
        htmlPluginData.body = htmlPluginData.body.map(<span class="hljs-function"><span class="hljs-params">tag</span> =&gt;</span> <span class="hljs-keyword">this</span>.processTag(compilation, tag));
        <span class="hljs-keyword">return</span> htmlPluginData;
    }
    processTag(compilation, tag) {
        <span class="hljs-keyword">let</span> inlineSource = <span class="hljs-keyword">this</span>.options.inlineSource;
        <span class="hljs-keyword">let</span> assetUrl;
        <span class="hljs-keyword">if</span> (tag.tagName == <span class="hljs-string">&apos;link&apos;</span> &amp;&amp; inlineSource.test(tag.attributes.href)) {
            assetUrl = tag.attributes.href;
            tag = {
                <span class="hljs-attr">tagName</span>: <span class="hljs-string">&apos;style&apos;</span>,
                <span class="hljs-attr">closeTag</span>: <span class="hljs-literal">true</span>,
                <span class="hljs-attr">attributes</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">&apos;text/css&apos;</span> }
            }
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (tag.tagName == <span class="hljs-string">&apos;script&apos;</span> &amp;&amp; inlineSource.test(tag.attributes.src)) {
            assetUrl = tag.attributes.src;
            tag = {
                <span class="hljs-attr">tagName</span>: <span class="hljs-string">&apos;script&apos;</span>,
                <span class="hljs-attr">closeTag</span>: <span class="hljs-literal">true</span>,
                <span class="hljs-attr">attributes</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">&apos;text/javascript&apos;</span> }
            }
        }
        <span class="hljs-keyword">if</span> (assetUrl) {
            <span class="hljs-keyword">let</span> asset = compilation.assets[assetUrl];
            tag.innerHTML = asset.source();
            <span class="hljs-keyword">delete</span> compilation.assets[assetUrl];
        }
        <span class="hljs-keyword">return</span> tag;
    }
}

<span class="hljs-built_in">module</span>.exports = InlineWebpackPlugin;
</code></pre>
<p>webpack.config.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> InlineWebpackPlugin = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./plugins/InlineWebpackPlugin&apos;</span>);
<span class="hljs-keyword">const</span> HtmlWebpackPlugin = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;html-webpack-plugin&apos;</span>);
<span class="hljs-keyword">const</span> MiniCssExtractPlugin = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;mini-css-extract-plugin&apos;</span>);
<span class="hljs-built_in">module</span>.exports = {
    <span class="hljs-attr">mode</span>: <span class="hljs-string">&apos;development&apos;</span>,
    <span class="hljs-attr">entry</span>: <span class="hljs-string">&quot;./src/index.js&quot;</span>,
    <span class="hljs-attr">output</span>: {
        <span class="hljs-attr">path</span>: resolve(<span class="hljs-string">&quot;dist&quot;</span>),
        <span class="hljs-attr">filename</span>: <span class="hljs-string">&quot;bundle.js&quot;</span>
    },
    <span class="hljs-attr">resolveLoader</span>: {
        <span class="hljs-attr">modules</span>: [path.resolve(<span class="hljs-string">&apos;./loaders&apos;</span>), <span class="hljs-string">&apos;node_modules&apos;</span>]
    },
    <span class="hljs-attr">module</span>: {
        <span class="hljs-attr">rules</span>: [
            {
                <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.css$/</span>,
                <span class="hljs-attr">use</span>: [{ <span class="hljs-attr">loader</span>: MiniCssExtractPlugin.loader }, <span class="hljs-string">&apos;css-loader&apos;</span>]
            }
        ]
    },
    <span class="hljs-attr">plugins</span>: [
        <span class="hljs-keyword">new</span> HtmlWebpackPlugin({
            <span class="hljs-attr">template</span>: <span class="hljs-string">&apos;./src/index.html&apos;</span>,
            <span class="hljs-attr">filename</span>: <span class="hljs-string">&apos;index.html&apos;</span>
        }),
        <span class="hljs-keyword">new</span> InlineWebpackPlugin({
            <span class="hljs-attr">inlineSource</span>: <span class="hljs-regexp">/\.(js|css)$/</span>
        }),
        <span class="hljs-keyword">new</span> MiniCssExtractPlugin({
            <span class="hljs-attr">filename</span>: <span class="hljs-string">&apos;[name].css&apos;</span>,
            <span class="hljs-attr">chunkFilename</span>: <span class="hljs-string">&apos;[id].css&apos;</span>
        })
    ]
}
</code></pre>
<h2 id="t109.&#x81EA;&#x52A8;&#x4E0A;&#x4F20;&#x8D44;&#x6E90;&#x6587;&#x4EF6;&#x5230;CDN">9.&#x81EA;&#x52A8;&#x4E0A;&#x4F20;&#x8D44;&#x6E90;&#x6587;&#x4EF6;&#x5230;CDN <a href="#t109.&#x81EA;&#x52A8;&#x4E0A;&#x4F20;&#x8D44;&#x6E90;&#x6587;&#x4EF6;&#x5230;CDN"> # </a></h2>
<ul>
<li><a href="https://developer.qiniu.com/kodo/sdk/1289/nodejs">qiniu</a></li>
</ul>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> qiniu = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;qiniu&apos;</span>);
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UploadPlugin</span> </span>{
    <span class="hljs-keyword">constructor</span>(options = {}) {
        <span class="hljs-keyword">let</span> { bucket = <span class="hljs-string">&apos;&apos;</span>, domain = <span class="hljs-string">&quot;&quot;</span>, accessKey = <span class="hljs-string">&apos;&apos;</span>, secretKey = <span class="hljs-string">&apos;&apos;</span> } = options;
        <span class="hljs-keyword">let</span> mac = <span class="hljs-keyword">new</span> qiniu.auth.digest.Mac(accessKey, secretKey);
        <span class="hljs-keyword">let</span> putPolicy = <span class="hljs-keyword">new</span> qiniu.rs.PutPolicy({ <span class="hljs-attr">scope</span>: bucket });
        <span class="hljs-keyword">this</span>.uploadToken = putPolicy.uploadToken(mac);
        <span class="hljs-keyword">let</span> config = <span class="hljs-keyword">new</span> qiniu.conf.Config();
        <span class="hljs-keyword">this</span>.formUploader = <span class="hljs-keyword">new</span> qiniu.form_up.FormUploader(config);
        <span class="hljs-keyword">this</span>.putExtra = <span class="hljs-keyword">new</span> qiniu.form_up.PutExtra();
    }
    apply(compiler) {
        compiler.hooks.afterEmit.tapPromise(<span class="hljs-string">&apos;UploadPlugin&apos;</span>, compilation =&gt; {
            <span class="hljs-keyword">let</span> assets = compilation.assets;
            <span class="hljs-keyword">let</span> promises = <span class="hljs-built_in">Object</span>.entries(assets).map(<span class="hljs-function">(<span class="hljs-params">[key, value]</span>) =&gt;</span> <span class="hljs-keyword">this</span>.upload(key, value.source()));
            <span class="hljs-keyword">return</span> <span class="hljs-built_in">Promise</span>.all(promises);
        });
    }
    upload(key, value) {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;value&apos;</span>, value)
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =&gt;</span> {
            <span class="hljs-keyword">this</span>.formUploader.put(<span class="hljs-keyword">this</span>.uploadToken, key, value, <span class="hljs-keyword">this</span>.putExtra, (err, body, info) =&gt; {
                err ? reject(err) : resolve(body);
            });
        });
    }
}
<span class="hljs-built_in">module</span>.exports = UploadPlugin;
</code></pre>
<pre><code class="lang-js"><span class="hljs-keyword">new</span> UploadPlugin({
    <span class="hljs-attr">bucket</span>: <span class="hljs-string">&apos;cnpmjs&apos;</span>,
    <span class="hljs-attr">domain</span>: <span class="hljs-string">&quot;img.zhufenpeixun.cn&quot;</span>,
    <span class="hljs-attr">accessKey</span>: <span class="hljs-string">&apos;fi5imW04AkxJItuFbbRy1ffH1HIoo17HbWOXw5fV&apos;</span>,
    <span class="hljs-attr">secretKey</span>: <span class="hljs-string">&apos;ru__Na4qIor4-V7U4AOJyp2KBUYEw1NWduiJ4Pby&apos;</span>
})
</code></pre>
<h2 id="t1110.&#x81EA;&#x52A8;&#x5916;&#x94FE;">10.&#x81EA;&#x52A8;&#x5916;&#x94FE; <a href="#t1110.&#x81EA;&#x52A8;&#x5916;&#x94FE;"> # </a></h2>
<h3 id="t1210.1 &#x4F7F;&#x7528;&#x5916;&#x90E8;&#x7C7B;&#x5E93;">10.1 &#x4F7F;&#x7528;&#x5916;&#x90E8;&#x7C7B;&#x5E93; <a href="#t1210.1 &#x4F7F;&#x7528;&#x5916;&#x90E8;&#x7C7B;&#x5E93;"> # </a></h3>
<ul>
<li>&#x624B;&#x52A8;&#x6307;&#x5B9A;  external</li>
<li>&#x624B;&#x52A8;&#x5F15;&#x5165; script</li>
</ul>
<blockquote>
<p>&#x80FD;&#x5426;&#x68C0;&#x6D4B;&#x4EE3;&#x7801;&#x4E2D;&#x7684;import&#x81EA;&#x52A8;&#x5904;&#x7406;&#x8FD9;&#x4E2A;&#x6B65;&#x9AA4;?</p>
</blockquote>
<h3 id="t1310.2 &#x601D;&#x8DEF;">10.2 &#x601D;&#x8DEF; <a href="#t1310.2 &#x601D;&#x8DEF;"> # </a></h3>
<p>&#x89E3;&#x51B3;import&#x81EA;&#x52A8;&#x5904;&#x7406;<code>external</code>&#x548C;<code>script</code>&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x9700;&#x8981;&#x600E;&#x4E48;&#x5B9E;&#x73B0;&#xFF0C;&#x8BE5;&#x4ECE;&#x54EA;&#x65B9;&#x9762;&#x5F00;&#x59CB;&#x8003;&#x8651;</p>
<ul>
<li><code>&#x4F9D;&#x8D56;</code> &#x5F53;&#x68C0;&#x6D4B;&#x5230;&#x6709;<code>import</code>&#x8BE5;<code>library</code>&#x65F6;&#xFF0C;&#x5C06;&#x5176;&#x8BBE;&#x7F6E;&#x4E3A;&#x4E0D;&#x6253;&#x5305;&#x7C7B;&#x4F3C;<code>exteral</code>,&#x5E76;&#x5728;&#x6307;&#x5B9A;&#x6A21;&#x7248;&#x4E2D;&#x52A0;&#x5165;script,&#x90A3;&#x4E48;&#x5982;&#x4F55;&#x68C0;&#x6D4B;import&#x90A3;&#xFF1F;&#x8FD9;&#x91CC;&#x5C31;&#x7528;<code>Parser</code></li>
<li><code>external&#x4F9D;&#x8D56;</code>
&#x9700;&#x8981;&#x4E86;&#x89E3;external&#x662F;&#x5982;&#x4F55;&#x5B9E;&#x73B0;&#x7684;&#xFF0C;webpack&#x7684;external&#x662F;&#x901A;&#x8FC7;&#x63D2;&#x4EF6;<code>ExternalsPlugin</code>&#x5B9E;&#x73B0;&#x7684;&#xFF0C;ExternalsPlugin&#x901A;&#x8FC7; tap NormalModuleFactory &#x5728;&#x6BCF;&#x6B21;&#x521B;&#x5EFA;Module&#x7684;&#x65F6;&#x5019;&#x5224;&#x65AD;&#x662F;&#x5426;&#x662F;ExternalModule</li>
<li>webpack4&#x52A0;&#x5165;&#x4E86;&#x6A21;&#x5757;&#x7C7B;&#x578B;&#x4E4B;&#x540E;&#xFF0C;<code>Parser</code>&#x83B7;&#x53D6;&#x9700;&#x8981;&#x6307;&#x5B9A;&#x7C7B;&#x578B;moduleType,&#x4E00;&#x822C;&#x4F7F;&#x7528;<code>javascript/auto</code>&#x5373;&#x53EF;</li>
</ul>
<h2 id="t1410.3 &#x4F7F;&#x7528;plugins">10.3 &#x4F7F;&#x7528;plugins <a href="#t1410.3 &#x4F7F;&#x7528;plugins"> # </a></h2>
<pre><code class="lang-js">plugins: [
        <span class="hljs-keyword">new</span> HtmlWebpackPlugin({
            <span class="hljs-attr">template</span>: <span class="hljs-string">&apos;./src/index.html&apos;</span>,
            <span class="hljs-attr">filename</span>:<span class="hljs-string">&apos;index.html&apos;</span>
        }),
        <span class="hljs-keyword">new</span> AutoExternalPlugin({
            <span class="hljs-attr">jquery</span>: {
                <span class="hljs-attr">expose</span>: <span class="hljs-string">&apos;$&apos;</span>,
                <span class="hljs-attr">url</span>: <span class="hljs-string">&apos;https://cdn.bootcss.com/jquery/3.1.0/jquery.js&apos;</span>
            }
        })
    ]
</code></pre>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> ExternalModule = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;webpack/lib/ExternalModule&apos;</span>);
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AutoExternalPlugin</span> </span>{
    <span class="hljs-keyword">constructor</span>(options) {
        <span class="hljs-keyword">this</span>.options = options;
        <span class="hljs-keyword">this</span>.externalModules = {};
    }
    apply(compiler) {
        <span class="hljs-comment">//1.&#x5728;&#x89E3;&#x6790;&#x8BED;&#x6CD5;&#x6811;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#x67E5;&#x627E;&#x90A3;&#x4E9B;&#x9700;&#x8981;&#x5916;&#x90E8;&#x5F15;&#x5165;&#x7684;&#x6A21;&#x5757;&#x540D;&#x79F0;</span>
        compiler.hooks.normalModuleFactory.tap(<span class="hljs-string">&apos;AutoExternalPlugin&apos;</span>, normalModuleFactory =&gt; {
            normalModuleFactory.hooks.parser
                .for(<span class="hljs-string">&apos;javascript/auto&apos;</span>)
                .tap(<span class="hljs-string">&apos;AutoExternalPlugin&apos;</span>, parser =&gt; {
                    parser.hooks.import.tap(<span class="hljs-string">&apos;AutoExternalPlugin&apos;</span>, (statement, source) =&gt; {
                        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.options[source])
                            <span class="hljs-keyword">this</span>.externalModules[source] = <span class="hljs-literal">true</span>;
                    });
                });
            <span class="hljs-comment">//2.&#x5728;&#x751F;&#x4EA7;&#x6A21;&#x5757;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#x53D1;&#x73B0;&#x5982;&#x679C;&#x662F;&#x5916;&#x90E8;&#x6A21;&#x5757;&#x5219;&#x8FD4;&#x56DE;&#x5916;&#x90E8;&#x6A21;&#x5757;</span>
            normalModuleFactory.hooks.factory.tap(<span class="hljs-string">&apos;AutoExternalPlugin&apos;</span>, factory =&gt; <span class="hljs-function">(<span class="hljs-params">data, callback</span>) =&gt;</span> {
                <span class="hljs-keyword">const</span> dependency = data.dependencies[<span class="hljs-number">0</span>];
                <span class="hljs-keyword">let</span> value = dependency.request;
                <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.externalModules[value]) {
                    <span class="hljs-keyword">let</span> varName = <span class="hljs-keyword">this</span>.options[value].expose;
                    callback(<span class="hljs-literal">null</span>, <span class="hljs-keyword">new</span> ExternalModule(varName, <span class="hljs-string">&apos;window&apos;</span>));
                } <span class="hljs-keyword">else</span> {
                    factory(data, callback);
                }
            });
        });
        compiler.hooks.compilation.tap(<span class="hljs-string">&apos;AutoExternalPlugin&apos;</span>, compilation =&gt; {
            <span class="hljs-comment">//3.&#x5411;body&#x5E95;&#x90E8;&#x63D2;&#x5165;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x7684;&#x811A;&#x672C;</span>
            compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync(<span class="hljs-string">&apos;normalModuleFactory&apos;</span>, (htmlPluginData, callback) =&gt; {
                <span class="hljs-built_in">Object</span>.values(<span class="hljs-keyword">this</span>.externalModules).forEach(<span class="hljs-function"><span class="hljs-params">src</span> =&gt;</span> {
                    htmlPluginData.body.unshift({
                        <span class="hljs-attr">tagName</span>: <span class="hljs-string">&apos;script&apos;</span>,
                        <span class="hljs-attr">closeTag</span>: <span class="hljs-literal">true</span>,
                        <span class="hljs-attr">attributes</span>: { <span class="hljs-attr">type</span>: <span class="hljs-string">&apos;text/javascript&apos;</span>, src }
                    });
                });
            });
        });
    }
}
<span class="hljs-built_in">module</span>.exports = AutoExternalPlugin;
</code></pre>
<h2 id="t15&#x53C2;&#x8003;">&#x53C2;&#x8003; <a href="#t15&#x53C2;&#x8003;"> # </a></h2>
<ul>
<li><a href="https://developer.qiniu.com/kodo/sdk/1289/nodejs">Node.js SDK</a></li>
<li><a href="https://webpack.js.org/contribute/writing-a-plugin/">writing-a-plugin</a></li>
<li><a href="https://webpack.js.org/api/plugins/">api/plugins</a></li>
<li><a href="http://www.css88.com/doc/webpack2/api/plugins/">&#x63D2;&#x4EF6; API</a></li>
</ul>
<h3 id="t16&#x6838;&#x5FC3;&#x6D41;&#x7A0B;">&#x6838;&#x5FC3;&#x6D41;&#x7A0B; <a href="#t16&#x6838;&#x5FC3;&#x6D41;&#x7A0B;"> # </a></h3>
<pre><code>- webpack-cli/cli.js   compiler = webpack(options);
- webpack/webpack.js compiler = `new Compiler`(options.context);
- webpack-cli/cli.js   compiler.`run`(compilerCallback);
- Compiler.js `run`(callback)
-             this.hooks.`beforeRun`.callAsync
-             this.hooks.`run`.callAsync
-             this.`readRecords`
-             this.`compile`(onCompiled);
-             const params = this.newCompilationParams();
-             this.hooks.`beforeCompile`.callAsync
-             this.hooks.`compile`.call(params);
-             const compilation = this.newCompilation(params);
-             this.hooks.`make`.callAsync
- SingleEntryPlugin.js compilation.addEntry(context, dep, name, callback);
- Compilation.js addEntry
-                this._addModuleChain
-                const moduleFactory = this.dependencyFactories.get(Dep);
-                moduleFactory.create
- NormalModuleFactory create(data, callback)
-                      this.hooks.`beforeResolve`.callAsync
-                      const factory = this.hooks.factory.call(null);
-                      factory(result, (err, module)
-                      let resolver = this.hooks.resolver.call(null);
-                      resolver(result
-                      this.hooks.resolver.tap(&quot;NormalModuleFactory&quot;
-                      const result = this.ruleSet.exec({
-                     getParser(type, parserOptions) 
-                     this.hooks.afterResolve.callAsync
-                     let createdModule = this.hooks.createModule.call(result);
-                     createdModule = new NormalModule(result);
-                     createdModule = this.hooks.`module`.call(createdModule, result); 
- Compilation.js      const addModuleResult = this.addModule(module);
-                     this.buildModule(module
-                     this.hooks.buildModule.call(module);
-                     module.build(
- NormalModule.js     build(options, compilation, resolver, fs, callback) 
-                     return this.doBuild(options, compilation, resolver, fs
-                     runLoaders()
-                     this._source = this.createSource(
-                     this._ast =
-                      const result = this.parser.parse(this._ast
- Compilation.js      this.hooks.succeedModule.call(module);
-                        this.processModuleDependencies
-                          if (module) 
- Compiler.js        compilation.finish()
-                    compilation.seal
-                    this.hooks.afterCompile 

</code></pre>
        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
